<!-- build time:Tue Dec 17 2019 19:26:41 GMT+0800 (GMT+08:00) --><!doctype html><html class="theme-next mist" lang="zh-Hans"><head><meta name="generator" content="Hexo 3.8.0"><meta name="google-site-verification" content="7Tau9WyVgxnsEY9oYedu9g0U6_8akOX3wiKbaYcrg9A"><meta name="baidu-site-verification" content="EVwLiaxdxX"><link href="/css/xps13.css" rel="stylesheet" type="text/css"><link href="/css/message.css" rel="stylesheet" type="text/css"><script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css"><meta name="keywords" content="Kubernetes,"><link rel="alternate" href="/atom.xml" title="MrBird" type="application/atom+xml"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1"><meta name="description" content="Pod是Kubernetes的最小调度单位，包含一个或者多个容器（比如Docker容器），容器间共享网络和存储。这节主要记录什么是静态Pod，Pod容器如何共享存储，如何使用ConfigMap管理Pod配置，如何使用Downward API获取Pod信息等。"><meta name="keywords" content="Kubernetes"><meta property="og:type" content="article"><meta property="og:title" content="Kubernetes Pod基础"><meta property="og:url" content="http://mrbird.cc/Kubernetes-Pod-Basic.html"><meta property="og:site_name" content="MrBird"><meta property="og:description" content="Pod是Kubernetes的最小调度单位，包含一个或者多个容器（比如Docker容器），容器间共享网络和存储。这节主要记录什么是静态Pod，Pod容器如何共享存储，如何使用ConfigMap管理Pod配置，如何使用Downward API获取Pod信息等。"><meta property="og:locale" content="zh-Hans"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103124437.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103124756.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103124916.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103143252.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103143458.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103145002.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103150049.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103150859.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103151524.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103151751.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103153532.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103154213.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103155731.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103162045.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103163059.png"><meta property="og:image" content="http://mrbird.cc/img/QQ截图20191103171025.png"><meta property="og:updated_time" content="2019-11-03T09:32:24.908Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Kubernetes Pod基础"><meta name="twitter:description" content="Pod是Kubernetes的最小调度单位，包含一个或者多个容器（比如Docker容器），容器间共享网络和存储。这节主要记录什么是静态Pod，Pod容器如何共享存储，如何使用ConfigMap管理Pod配置，如何使用Downward API获取Pod信息等。"><meta name="twitter:image" content="http://mrbird.cc/img/QQ截图20191103124437.png"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Mist",sidebar:{position:"left",display:"always",offset:12,offset_float:0,b2t:!1,scrollpercent:!1},fancybox:!1,motion:!1}</script><title>Kubernetes Pod基础 | MrBird</title></head><body ondragstart="return!1" class="animsition" lang="zh-Hans" style="overflow-x:hidden;padding-right:280px"><script type="text/javascript" src="/js/mo.min.js"></script><style>@media (min-width:768px) and (max-width:991px){body .header-innerr{width:700px!important}}.header-innerr{margin:0 auto;padding:100px 0 70px;width:880px}@media (min-width:1600px){.container .header-innerr{width:1200px}}.header-innerr{position:relative}.header-innerr{padding:20px 0 0}.header-innerr:after,.header-innerr:before{content:" ";display:table}.header-innerr:after{clear:both}@media (max-width:767px){.header-innerr{width:auto;padding:10px;margin-bottom:-20px}}</style><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><link href="https://fonts.font.im/css?family=Merienda" rel="stylesheet"><div class="custom-logo-site-title"><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title" style="font-family:Merienda;font-size:1.3rem">MrBird</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle"></p></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section">HOME</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">ARCHIVES</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section">TAGS</a></li><li class="menu-item menu-item-friends"><a href="/friends/" rel="section">FRIENDS</a></li><div class="sidebar-toggle" style="display:none"><div class="sidebar-toggle-line-wrap"><span class="sidebar-toggle-line sidebar-toggle-line-first"></span> <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span> <span class="sidebar-toggle-line sidebar-toggle-line-last"></span></div></div></ul><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="Search" spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div><div class="header-innerr"><div class="note info" style="margin:0;letter-spacing:.15px">🐤手把手教你搭建<strong>Spring Cloud微服务权限系统</strong>（从零到部署）：<a style="color:#40dab2;font-weight:600" href="https://www.kancloud.cn/mrbird/spring-cloud" target="_blank">https://www.kancloud.cn/mrbird/spring-cloud</a></div></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://mrbird.cc/Kubernetes-Pod-Basic.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="MrBird"><meta itemprop="description" content=""><meta itemprop="image" content="/images/blogImage.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="MrBird"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">Kubernetes Pod基础</h1><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">Posted on</span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-03T10:50:14+08:00">2019-11-03 </time></span><span></span> <span class="post-meta-divider">|</span> <span class="page-pv"><i class="fa fa-laptop"></i>&nbsp;&nbsp;Visit count <span class="busuanzi-value" id="busuanzi_value_page_pv"></span></span></div></header><div class="post-body" itemprop="articleBody"><p>Pod是Kubernetes的最小调度单位，包含一个或者多个容器（比如Docker容器），容器间共享网络和存储。这节主要记录什么是静态Pod，Pod容器如何共享存储，如何使用ConfigMap管理Pod配置，如何使用Downward API获取Pod信息等。<a id="more"></a></p><h2 id="静态Pod"><a href="#静态Pod" class="headerlink" title="静态Pod"></a>静态Pod</h2><p>静态Pod是由kubelet创建并管理的特殊的Pod，无法和Pod管理对象关联，并且不能通过API Server关联。创建静态Pod有配置文件方式和HTTP方式：</p><h3 id="配置文件方式"><a href="#配置文件方式" class="headerlink" title="配置文件方式"></a>配置文件方式</h3><p>在搭建Kubernetes集群的时候，从启动Master节点的日志可以看出，静态Pod的目录位于<code>/etc/kubernetes/manifests</code>：</p><p>在该目录下创建静态Pod文件：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">cd /etc/kubernetes/manifests</span><br><span class="line">vim static-pod.yaml</span><br></pre></td></tr></table></figure><p></p><p>内容如下所示：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">static-pod</span></span><br><span class="line"><span class="attr">  labels:</span></span><br><span class="line"><span class="attr">    name:</span> <span class="string">static-pod</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">static-nginx</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">nginx</span></span><br><span class="line"><span class="attr">      imagePullPolicy:</span> <span class="string">IfNotPresent</span></span><br><span class="line"><span class="attr">      ports:</span></span><br><span class="line"><span class="attr">        - containerPort:</span> <span class="number">80</span></span><br></pre></td></tr></table></figure><p></p><p>过了一会查看Pod：</p><p><img src="img/QQ截图20191103124437.png" alt="QQ截图20191103124437.png"></p><p>于静态Pod无法通过API Server直接管理，所以在Master上尝试删除这个Pod时，会使其变成Pending状态，且不会被删除。</p><p><img src="img/QQ截图20191103124756.png" alt="QQ截图20191103124756.png"></p><p>删除该Pod的操作只能是到其所在Node上将其定义文件static-pod.yaml从/etc/kubernetes/manifests目录下删除：</p><p><img src="img/QQ截图20191103124916.png" alt="QQ截图20191103124916.png"></p><h3 id="HTTP方式"><a href="#HTTP方式" class="headerlink" title="HTTP方式"></a>HTTP方式</h3><p>过设置kubelet的启动参数<code>--manifest-url</code>，kubelet将会定期从该URL地址下载Pod的定义文件，并以.yaml或.json文件的格式进行解析，然后创建Pod。</p><h2 id="Pod容器共享Volume"><a href="#Pod容器共享Volume" class="headerlink" title="Pod容器共享Volume"></a>Pod容器共享Volume</h2><p>同一个Pod的多个容器间可以共享Pod级别的Volume，举个例子：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vim pod-volume.yml</span><br></pre></td></tr></table></figure><p></p><p>内容如下所示：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">pod-volume</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">tomcat</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">tomcat</span></span><br><span class="line"><span class="attr">      imagePullPolicy:</span> <span class="string">IfNotPresent</span></span><br><span class="line"><span class="attr">      ports:</span></span><br><span class="line"><span class="attr">        - containerPort:</span> <span class="number">8080</span></span><br><span class="line"><span class="attr">      volumeMounts:</span></span><br><span class="line"><span class="attr">        - mountPath:</span> <span class="string">/usr/local/tomcat/logs</span></span><br><span class="line"><span class="attr">          name:</span> <span class="string">logs</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      imagePullPolicy:</span> <span class="string">IfNotPresent</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">,</span> <span class="string">"tail -f /logs/catalina*.log"</span><span class="string">]</span></span><br><span class="line"><span class="attr">      volumeMounts:</span></span><br><span class="line"><span class="attr">        - mountPath:</span> <span class="string">/logs</span></span><br><span class="line"><span class="attr">          name:</span> <span class="string">logs</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">logs</span></span><br><span class="line"><span class="attr">      emptyDir:</span> <span class="string">&#123;&#125;</span></span><br></pre></td></tr></table></figure><p></p><p>上面Pod定义中，创建了一个Pod级别的Volume，名称为logs，类型为emptyDir。这个Volume同时挂载到了tomcat的/usr/local/tomcat/logs目录下，也挂载到了busybox的/logs目录下。</p><p>创建该Pod：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create -f pod-volume.yml</span><br></pre></td></tr></table></figure><p></p><div class="note info"><p>这里的tomcat镜像比较大，大概有500MB左右，所以在创建之前，最好在Kubernetes集群的每个节点中配置Docker镜像加速地址。</p></div><p>当pod-volume状态为ready后，查看busybox的日志：</p><p><img src="img/QQ截图20191103143252.png" alt="QQ截图20191103143252.png"></p><p>该日志为tomcat的启动日志，说明上面挂载的Volume生效了，可以通过查看tomcat<code>/usr/local/tomcat/logs</code>目录下和busybox<code>/logs</code>目录下的内容来证明这一点：</p><p><img src="img/QQ截图20191103143458.png" alt="QQ截图20191103143458.png"></p><h2 id="ConfigMap"><a href="#ConfigMap" class="headerlink" title="ConfigMap"></a>ConfigMap</h2><p>ConfigMap以一个或多个key:value的形式保存在Kubernetes系统中供应用使用，既可以用于表示一个变量的值（例如version=v1），也可以用于表示一个完整配置文件的内容（例如<code>server.xml=&lt;?xml...&gt;...</code>）。</p><h3 id="创建ConfigMap"><a href="#创建ConfigMap" class="headerlink" title="创建ConfigMap"></a>创建ConfigMap</h3><p>创建ConfigMap主要有两种方式：</p><p><strong>1.通过yml文件创建</strong></p><p>创建<code>simple-cm.yml</code>文件，内容如下所示：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">ConfigMap</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">simple-cm</span></span><br><span class="line"><span class="attr">data:</span></span><br><span class="line"><span class="attr">  version:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">  release:</span> <span class="string">stable</span></span><br></pre></td></tr></table></figure><p></p><p>该ConfigMap仅包含两个简单的值version和releases。</p><p>创建该ConfigMap：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl create -f simple-cm.yml</span><br></pre></td></tr></table></figure><p></p><p>查看该ConfigMap：</p><p><img src="img/QQ截图20191103145002.png" alt="QQ截图20191103145002.png"></p><p>在定义ConfigMap的时候，value除了可以使用简单的值外，还可以是整个配置文件的内容。</p><p>创建<code>file-cm.yml</code>，内容如下所示：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">ConfigMap</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">file-cm</span></span><br><span class="line"><span class="attr">data:</span></span><br><span class="line"><span class="attr">  serverXml:</span> <span class="string">|</span></span><br><span class="line"><span class="string">    &lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="string">    &lt;Server port="8005" shutdown="SHUTDOWN"&gt;</span></span><br><span class="line"><span class="string">      &lt;Listener className="org.apache.catalina.startup.VersionLoggerListener"/&gt;</span></span><br><span class="line"><span class="string">      &lt;Listener SSLEngine="on" className="org.apache.catalina.core.AprLifecycleListener"/&gt;</span></span><br><span class="line"><span class="string">      &lt;!-- Prevent memory leaks due to use of particular java/javax APIs--&gt;</span></span><br><span class="line"><span class="string">      &lt;Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/&gt;</span></span><br><span class="line"><span class="string">      &lt;Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/&gt;</span></span><br><span class="line"><span class="string">      &lt;Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/&gt;</span></span><br><span class="line"><span class="string">      &lt;GlobalNamingResources&gt;</span></span><br><span class="line"><span class="string">        &lt;Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/&gt;</span></span><br><span class="line"><span class="string">      &lt;/GlobalNamingResources&gt;</span></span><br><span class="line"><span class="string">      &lt;Service name="Catalina"&gt;</span></span><br><span class="line"><span class="string">        &lt;Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/&gt;</span></span><br><span class="line"><span class="string">        &lt;Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/&gt;</span></span><br><span class="line"><span class="string">        &lt;Engine defaultHost="localhost" name="Catalina"&gt;</span></span><br><span class="line"><span class="string">          &lt;Realm className="org.apache.catalina.realm.LockOutRealm"&gt;</span></span><br><span class="line"><span class="string">            &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/&gt;</span></span><br><span class="line"><span class="string">          &lt;/Realm&gt;</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">          &lt;Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"&gt;</span></span><br><span class="line"><span class="string">            &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" prefix="localhost_access_log" suffix=".txt"/&gt;</span></span><br><span class="line"><span class="string">          &lt;Context docBase="D:\Code\apache-tomcat-8.5.32\webapps\tj_certification" path="/tj_certification" reloadable="true" source="org.eclipse.jst.jee.server:tj_certification"/&gt;&lt;/Host&gt;</span></span><br><span class="line"><span class="string">        &lt;/Engine&gt;</span></span><br><span class="line"><span class="string">      &lt;/Service&gt;</span></span><br><span class="line"><span class="string">    &lt;/Server&gt;</span></span><br><span class="line"><span class="string"></span><span class="attr">  serverProperties:</span> <span class="string">"1catalina.org.apache.juli.AsyncFileHandler.level = FINE</span></span><br><span class="line"><span class="string">                     1catalina.org.apache.juli.AsyncFileHandler.directory = $&#123;catalina.base&#125;/logs</span></span><br><span class="line"><span class="string">                     1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     2localhost.org.apache.juli.AsyncFileHandler.level = FINE</span></span><br><span class="line"><span class="string">                     2localhost.org.apache.juli.AsyncFileHandler.directory = $&#123;catalina.base&#125;/logs</span></span><br><span class="line"><span class="string">                     2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     3manager.org.apache.juli.AsyncFileHandler.level = FINE</span></span><br><span class="line"><span class="string">                     3manager.org.apache.juli.AsyncFileHandler.directory = $&#123;catalina.base&#125;/logs</span></span><br><span class="line"><span class="string">                     3manager.org.apache.juli.AsyncFileHandler.prefix = manager.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     4host-manager.org.apache.juli.AsyncFileHandler.level = FINE</span></span><br><span class="line"><span class="string">                     4host-manager.org.apache.juli.AsyncFileHandler.directory = $&#123;catalina.base&#125;/logs</span></span><br><span class="line"><span class="string">                     4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     java.util.logging.ConsoleHandler.level = FINE</span></span><br><span class="line"><span class="string">                     java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO</span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO</span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler</span></span><br><span class="line"><span class="string"></span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO</span></span><br><span class="line"><span class="string">                     org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler"</span></span><br></pre></td></tr></table></figure><p></p><p>创建该ConfigMap：</p><p><img src="img/QQ截图20191103150049.png" alt="QQ截图20191103150049.png"></p><p><strong>2.直接通过Kubectl命令创建</strong></p><p>通过kubectl命令创建ConfigMap主要有以下三种用法：</p><ol><li><p>通过–from-file参数从文件中进行创建，可以指定key的名称，也可以在一个命令行中创建包含多个key的ConfigMap，语法为：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl cerate configmap [NAME] --from-file=[key=]source --from-file=[key=]source</span><br></pre></td></tr></table></figure></li><li><p>通过–from-file参数从目录中进行创建，该目录下的每个配置文件名都被设置为key，文件的内容被设置为value，语法为：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl cerate configmap [NAME] --from-file=config-file-dir</span><br></pre></td></tr></table></figure></li><li><p>使用–from-literal时会从文本中进行创建，直接将指定的key#=value#创建为ConfigMap的内容，语法为：</p><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">kubectl cerate configmap [NAME] --from-literal=key1=value1 --from-literal=key2=value2</span><br></pre></td></tr></table></figure></li></ol><p>比如使用kubectl命令创建一个和simple-cm效果一样的ConfigMap：</p><p><img src="img/QQ截图20191103150859.png" alt="QQ截图20191103150859.png"></p><p>使用kubectl命令创建一个和file-cm效果一样的ConfigMap：</p><p>首先在当前目录下准备好两个配置文件server.xml和server.properties：</p><p><img src="img/QQ截图20191103151524.png" alt="QQ截图20191103151524.png"></p><p>然后使用kubectl命令创建：</p><p><img src="img/QQ截图20191103151751.png" alt="QQ截图20191103151751.png"></p><h3 id="Pod容器使用ConfigMap"><a href="#Pod容器使用ConfigMap" class="headerlink" title="Pod容器使用ConfigMap"></a>Pod容器使用ConfigMap</h3><p>Pod的容器要使用ConfigMap主要有两种方式：</p><ol><li>通过环境变量获取ConfigMap中的内容;</li><li>通过Volume挂载的方式将ConfigMap中的内容挂载为容器内部的文件或目录。</li></ol><p><strong>通过环境变量的方式</strong></p><p>创建一个Pod配置（simple-cm-pod.yml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">simple-cm-pod</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">,</span> <span class="string">"env | grep ENV"</span><span class="string">]</span> <span class="comment"># 打印名称包含ENV的环境变量</span></span><br><span class="line"><span class="attr">      env:</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">ENVVERSION</span>     <span class="comment"># 定义环境变量名称</span></span><br><span class="line"><span class="attr">          valueFrom:</span>           <span class="comment"># 值来自...</span></span><br><span class="line"><span class="attr">            configMapKeyRef:</span>   <span class="comment"># ConfigMap键的引用</span></span><br><span class="line"><span class="attr">              key:</span> <span class="string">version</span>     <span class="comment"># ConfigMap的key</span></span><br><span class="line"><span class="attr">              name:</span> <span class="string">simple-cm</span>  <span class="comment"># ConfigMap的名称</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">ENVRELEASE</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            configMapKeyRef:</span></span><br><span class="line"><span class="attr">              key:</span> <span class="string">release</span></span><br><span class="line"><span class="attr">              name:</span> <span class="string">simple-cm</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod，并查看日志：</p><p><img src="img/QQ截图20191103153532.png" alt="QQ截图20191103153532.png"></p><p>可以看到，值已经成功从ConfigMap里去到了。</p><p>如果要引用某个ConfigMap的所有内容，可以使用下面这种方式。定义一个Pod配置（simple-cm-pod-all.uyml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">simple-cm-pod-all</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">,</span> <span class="string">"env"</span><span class="string">]</span></span><br><span class="line"><span class="attr">      envFrom:</span></span><br><span class="line"><span class="attr">        - configMapRef:</span></span><br><span class="line"><span class="attr">            name:</span> <span class="string">simple-cm</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod，并查看日志：</p><p><img src="img/QQ截图20191103154213.png" alt="QQ截图20191103154213.png"></p><p><strong>通过Volume挂载的方式</strong></p><p>创建一个Pod配置（file-cm-pod.yml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">file-cm-pod</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">tomcat</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">tomcat</span></span><br><span class="line"><span class="attr">      ports:</span></span><br><span class="line"><span class="attr">        - containerPort:</span> <span class="number">8080</span></span><br><span class="line"><span class="attr">      volumeMounts:</span></span><br><span class="line"><span class="attr">        - mountPath:</span> <span class="string">/configs</span> <span class="comment"># 容器挂载目录为/configs</span></span><br><span class="line"><span class="attr">          name:</span> <span class="string">config-vm</span> <span class="comment"># 引用下面定义的这个Volume</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">config-vm</span>   <span class="comment"># volume名称为config-vm</span></span><br><span class="line"><span class="attr">      configMap:</span>        <span class="comment"># 通过ConfigMap获取</span></span><br><span class="line"><span class="attr">        name:</span> <span class="string">file-cm</span>   <span class="comment"># 引用名称为file-cm的ConfigMap</span></span><br><span class="line"><span class="attr">        items:</span></span><br><span class="line"><span class="attr">          - key:</span> <span class="string">serverXml</span> <span class="comment"># ConfigMap里配置的key</span></span><br><span class="line"><span class="attr">            path:</span> <span class="string">server.xml</span> <span class="comment"># 值使用server.xml文件进行挂载</span></span><br><span class="line"><span class="attr">          - key:</span> <span class="string">serverProperties</span></span><br><span class="line"><span class="attr">            path:</span> <span class="string">server.properties</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod，并进入到容器内部观察/configs目录下文件内容：</p><p><img src="img/QQ截图20191103155731.png" alt="QQ截图20191103155731.png"></p><p>可以看到名称为file-cm的ConfigMap内容已经成功挂载到了tomcat容器内部。</p><p>如果在引用ConfigMap时不指定items，则使用volumeMount方式在容器内的目录下为每个item都生成一个文件名为key的文件。</p><div class="note danger"><p>在Pod对ConfigMap进行挂载（volumeMount）操作时，在容器内部只能挂载为“目录”，无法挂载为“文件”。在挂载到容器内部后，在目录下将包含ConfigMap定义的每个item，如果在该目录下原来还有其他文件，则容器内的该目录将被挂载的ConfigMap<strong>覆盖</strong>。</p></div><h2 id="Downward-API"><a href="#Downward-API" class="headerlink" title="Downward API"></a>Downward API</h2><p>Downward API用于将Pod相关信息注入到容器内部，主要有<strong>环境变量</strong>和<strong>Volume挂载</strong>两种方式。</p><p><strong>环境变量</strong></p><p>创建一个Pod配置（dapi-pod.yml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">dapi-pod</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">,</span> <span class="string">"env | grep MY_POD"</span><span class="string">]</span></span><br><span class="line"><span class="attr">      env:</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_POD_NAME</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            fieldRef:</span></span><br><span class="line"><span class="attr">              fieldPath:</span> <span class="string">metadata.name</span> <span class="comment"># 通过downward api获取当前pod名称</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_POD_NAMESPACE</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            fieldRef:</span></span><br><span class="line"><span class="attr">              fieldPath:</span> <span class="string">metadata.namespace</span>  <span class="comment"># 通过downward api获取当前pod namespace</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_POD_IP</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            fieldRef:</span></span><br><span class="line"><span class="attr">              fieldPath:</span> <span class="string">status.podIP</span>  <span class="comment"># 通过downward api获取当前pod IP</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod，并查看日志：</p><p><img src="img/QQ截图20191103162045.png" alt="QQ截图20191103162045.png"></p><p>通过环境变量的方式还可以将容器的requests和limits信息注入到容器的环境变量中，创建一个Pod配置（dapi-pod-container-vars.yml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">dapi-pod-container-vars</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">]</span></span><br><span class="line"><span class="attr">      args:</span></span><br><span class="line"><span class="bullet">        -</span> <span class="string">while</span> <span class="literal">true</span><span class="string">;do</span></span><br><span class="line">            <span class="string">echo</span> <span class="bullet">-en</span> <span class="string">'\n'</span><span class="string">;</span></span><br><span class="line">            <span class="string">printenv</span> <span class="string">MY_CPU_REQUEST</span> <span class="string">MY_CPU_LIMIT;</span></span><br><span class="line">            <span class="string">printenv</span> <span class="string">MY_MEM_REQUEST</span> <span class="string">MY_MEM_LIMIT;</span></span><br><span class="line">            <span class="string">sleep</span> <span class="number">3600</span><span class="string">;</span></span><br><span class="line">          <span class="string">done;</span></span><br><span class="line"><span class="attr">      resources:</span></span><br><span class="line"><span class="attr">        requests:</span></span><br><span class="line"><span class="attr">          memory:</span> <span class="string">"32Mi"</span></span><br><span class="line"><span class="attr">          cpu:</span> <span class="string">"125m"</span></span><br><span class="line"><span class="attr">        limits:</span></span><br><span class="line"><span class="attr">          memory:</span> <span class="string">"64Mi"</span></span><br><span class="line"><span class="attr">          cpu:</span> <span class="string">"250m"</span></span><br><span class="line"><span class="attr">      env:</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_CPU_REQUEST</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            resourceFieldRef:</span></span><br><span class="line"><span class="attr">              resource:</span> <span class="string">requests.cpu</span></span><br><span class="line"><span class="attr">              containerName:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_CPU_LIMIT</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            resourceFieldRef:</span></span><br><span class="line"><span class="attr">              resource:</span> <span class="string">limits.cpu</span></span><br><span class="line"><span class="attr">              containerName:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_MEM_REQUEST</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            resourceFieldRef:</span></span><br><span class="line"><span class="attr">              resource:</span> <span class="string">request.memory</span></span><br><span class="line"><span class="attr">              containerName:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">        - name:</span> <span class="string">MY_MEM_LIMIT</span></span><br><span class="line"><span class="attr">          valueFrom:</span></span><br><span class="line"><span class="attr">            resourceFieldRef:</span></span><br><span class="line"><span class="attr">              resource:</span> <span class="string">limits.memory</span></span><br><span class="line"><span class="attr">              containerName:</span> <span class="string">busybox</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod并观察日志：</p><p><img src="img/QQ截图20191103163059.png" alt="QQ截图20191103163059.png"></p><p><strong>通过Volume挂载</strong></p><p>我们可以通过Downward API将Pod的Label，Annotation等信息挂载到容器内部文件中，新建一个Pod配置（dapi-pod-volumes.yml）：</p><figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">apiVersion:</span> <span class="string">v1</span></span><br><span class="line"><span class="attr">kind:</span> <span class="string">Pod</span></span><br><span class="line"><span class="attr">metadata:</span></span><br><span class="line"><span class="attr">  name:</span> <span class="string">dapi-pod-volume</span></span><br><span class="line"><span class="attr">  labels:</span></span><br><span class="line"><span class="attr">    tier:</span> <span class="string">frontend</span></span><br><span class="line"><span class="attr">    release:</span> <span class="string">canary</span></span><br><span class="line"><span class="attr">    environment:</span> <span class="string">dev</span></span><br><span class="line"><span class="attr">  annotations:</span></span><br><span class="line"><span class="attr">    builder:</span> <span class="string">mrbird</span></span><br><span class="line"><span class="attr">    blog:</span> <span class="attr">https://mrbird.cc</span></span><br><span class="line"><span class="attr">spec:</span></span><br><span class="line"><span class="attr">  containers:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      image:</span> <span class="string">busybox</span></span><br><span class="line"><span class="attr">      command:</span> <span class="string">["sh",</span> <span class="string">"-c"</span><span class="string">,</span> <span class="string">"sleep 36000"</span><span class="string">]</span></span><br><span class="line"><span class="attr">      volumeMounts:</span></span><br><span class="line"><span class="attr">        - mountPath:</span> <span class="string">/podinfo</span> <span class="comment"># 挂载路径</span></span><br><span class="line"><span class="attr">          name:</span> <span class="string">pod-info</span></span><br><span class="line"><span class="attr">  volumes:</span></span><br><span class="line"><span class="attr">    - name:</span> <span class="string">pod-info</span></span><br><span class="line"><span class="attr">      downwardAPI:</span> <span class="comment"># 通过downward api获取pod labels和annations信息</span></span><br><span class="line"><span class="attr">        items:</span></span><br><span class="line"><span class="attr">          - path:</span> <span class="string">"labels"</span> <span class="comment"># 挂载文件名称</span></span><br><span class="line"><span class="attr">            fieldRef:</span></span><br><span class="line"><span class="attr">              fieldPath:</span> <span class="string">metadata.labels</span> <span class="comment"># 挂载内容</span></span><br><span class="line"><span class="attr">          - path:</span> <span class="string">"annotation"</span></span><br><span class="line"><span class="attr">            fieldRef:</span></span><br><span class="line"><span class="attr">              fieldPath:</span> <span class="string">metadata.annotations</span></span><br></pre></td></tr></table></figure><p></p><p>创建该Pod，并进入到容器/podinfo目录观察结果：</p><p><img src="img/QQ截图20191103171025.png" alt="QQ截图20191103171025.png"></p><h2 id="Pod生命周期"><a href="#Pod生命周期" class="headerlink" title="Pod生命周期"></a>Pod生命周期</h2><table><thead><tr><th>阶段</th><th>描述</th></tr></thead><tbody><tr><td><strong>Pending</strong></td><td>Pod 已被 Kubernetes 接受，但尚未创建一个或多个容器镜像。这包括被调度之前的时间以及通过网络下载镜像所花费的时间，执行需要一段时间。</td></tr><tr><td><strong>Running</strong></td><td>Pod 已经被绑定到了一个节点，所有容器已被创建。至少一个容器正在运行，或者正在启动或重新启动。</td></tr><tr><td><strong>Succeeded</strong></td><td>所有容器成功终止，也不会重启。</td></tr><tr><td><strong>Failed</strong></td><td>所有容器终止，至少有一个容器以失败方式终止。也就是说，这个容器要么已非 0 状态退出，要么被系统终止。</td></tr><tr><td><strong>Unknown</strong></td><td>由于一些原因，Pod 的状态无法获取，通常是与 Pod 通信时出错导致的。</td></tr></tbody></table><p>三种重启策略：</p><ol><li>Always：当容器失效时，由kubelet自动重启该容器；</li><li>OnFailure：当容器终止运行且退出码不为0时，由kubelet自动重启该容器；</li><li>Never：不论容器运行状态如何，kubelet都不会重启该容器。</li></ol><p>结合Pod的状态和重启策略，以下为一些常见的状态转换场景：</p><table align="center" border="1" cellpadding="1" cellspacing="1"><tbody><tr><td colspan="1" rowspan="2" style="text-align:center;width:107px">Pod包含的容器数</td><td colspan="1" rowspan="2" style="text-align:center;width:108px">Pod当前的状态</td><td colspan="1" rowspan="2" style="text-align:center;width:139px">发生事件</td><td colspan="3" rowspan="1" style="text-align:center;width:486px">Pod的结果状态</td></tr><tr><td style="width:137px">RestarPolicy=Always</td><td style="width:162px">RestartPolicy=OnFailure</td><td style="width:180px">RestartPolicy=Never</td></tr><tr><td style="width:107px">包含1个容器</td><td style="width:108px">Running</td><td style="width:139px">容器成功退出</td><td style="width:137px">Running</td><td style="width:162px">Succeeded</td><td style="width:180px">Succeeded</td></tr><tr><td style="width:107px">包含1个容器</td><td style="width:108px">Running</td><td style="width:139px">容器失败退出</td><td style="width:137px">Running</td><td style="width:162px">Running</td><td style="width:180px">Failed</td></tr><tr><td style="width:107px">包含两个容器</td><td style="width:108px">Running</td><td style="width:139px">1个容器失败退出</td><td style="width:137px">Running</td><td style="width:162px">Running</td><td style="width:180px">Running</td></tr><tr><td style="width:107px">包含两个容器</td><td style="width:108px">Running</td><td style="width:139px">容器被OOM杀掉</td><td style="width:137px">Running</td><td style="width:162px">Running</td><td style="width:180px">Failed</td></tr></tbody></table><blockquote><p>《Kubernetes权威指南(第4版)》读书笔记</p></blockquote><script>$(".post-body a").not(".thispage").addClass("ignore-href").attr("target","_blank")</script></div><div></div><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center;color:#878787"><div>请作者喝瓶肥宅水~</div><button id="rewardButton" style="margin-top:10px" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span style="height:46px;width:46px;line-height:46px;border-radius:50%;color:#fe5f55;font-weight:600;background:#ffd5be;border:none;box-shadow:0 4px 8px 0 rgba(255,213,190,.4)">￥</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/img/wechat_pay.png" alt="MrBird WeChat Pay"></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/img/ali_pay.png" alt="MrBird Alipay"></div></div></div><style>#QR img{width:auto;margin:.8em 1em 0 1em}</style></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者：</strong> MrBird</li><li class="post-copyright-link"><strong>本文链接：</strong> <a href="http://mrbird.cc/Kubernetes-Pod-Basic.html" title="Kubernetes Pod基础">http://mrbird.cc/Kubernetes-Pod-Basic.html</a></li><li class="post-copyright-license"><strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处！</li></ul></div><footer class="post-footer"><div class="post-tags" style="margin-bottom:1.3rem"><a href="/tags/Kubernetes/" rel="tag"># Kubernetes</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/Kubernetes-Basic.html" rel="next" title="Kubernetes基础"><i class="fa fa-chevron-left"></i> Kubernetes基础</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/Kubernetes-Pod-Mananger.html" rel="prev" title="Kubernetes Pod管理对象与调度策略">Kubernetes Pod管理对象与调度策略 <i class="fa fa-chevron-right"></i></a></div></div></footer></article><hr><div id="container"></div><div class="post-spread"><div id="comment-div"></div><style>.valine .vlist{margin-bottom:3rem}.valine .vwrap .vcontrol .col.col-60{text-align:left}.valine .vlist .vcard .vhead,.valine .vlist .vcard section .vfooter{text-align:left}.valine .vlist .vcard section{padding-bottom:.5rem!important}.vname{color:#42b983!important}.valine .vinfo .col{text-align:left;margin-left:-27rem}div#comment-div{margin-bottom:-8rem}.valine .vlist .vcard .vcontent .code,.valine .vlist .vcard .vcontent code,.valine .vlist .vcard .vcontent pre{background:#fbfbfb}.valine .vlist .vcard .vcontent a{color:#42b983}.valine .vlist .vcard .vimg{border-radius:3px}.valine .vbtn{border-radius:2px;padding:.3rem 1.25rem}.valine .vbtn:active,.valine .vbtn:hover{color:#42b983;border-color:#42b983;background-color:#fff}.valine .vwrap .vheader .vinput:focus{border-bottom-color:#42b983}.valine .vlist .vcard .vcontent.expand:before{background:-webkit-gradient(linear,left top,left bottom,from(hsla(0,0%,100%,0)),to(hsla(0,0%,100%,.2)));background:linear-gradient(180deg,hsla(0,0%,100%,0),hsla(0,0%,100%,.2))}.valine .vlist .vcard .vcontent.expand:after{content:"点击展开";font-size:.4rem;text-align:right;left:-1rem;background:hsla(0,0%,100%,.2)}.valine .vlist .vcard section .vfooter .vat{color:#b3b3b3}.valine .vlist .vcard section .vfooter .vat:hover{color:#42b983}.vcontent img{margin:0}.valine .info{display:none}</style><script type="text/javascript" src="/js/av.min.js"></script><script type="text/javascript" src="/js/Valine.min.js"></script><script type="text/javascript" src="/js/activate-power-mode.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE),new Valine({el:"#comment-div",notify:!1,verify:!0,appId:"SMcDFP1bN1jgb9Lo8JmtICHm-gzGzoHsz",appKey:"dH4nrUrt3V5XiJiI6Qyejnbi",placeholder:"",path:window.location.pathname,avatar:"monsterid",guest_info:["nick","mail","link"]});var chicken='<a href="#"><img src="https://image.uisdc.com/wp-content/uploads/2018/06/uisdc-chat-chicken.gif" class="checken"></a>';$("#comment-div").prepend(chicken)</script></div></div><script>var $bqinline=$("img[alt='bq-inline']");$bqinline.css({width:"2.3rem",height:"2.3rem",display:"inline","vertical-align":"text-bottom"})</script></div><div class="comments" id="comments"></div></div><aside id="sidebar" class="sidebar" onselectstart="return!1"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">Contents</li><li class="sidebar-nav-overview" data-target="site-overview">Site Preview</li></ul><section class="site-overview sidebar-panel"><div class="sidebar-sticky sticky"><div itemscope itemtype="http://schema.org/Person"><div class="author__avatar"><img src="/images/blogImage.jpg" class="author__avatar" alt="MrBird" itemprop="image"></div><div class="author__content"><h3 class="author__name" itemprop="name">MrBird's Blog</h3><p class="author__bio" itemprop="description">A simple blog, code repository, just keep blogging</p></div><div class="author__urls-wrapper"><button class="btn btn--inverse">Follow</button><ul class="author__urls social-icons"><li><a href="http://map.baidu.com/?newmap=1&s=s%26wd%3D%E7%A6%8F%E5%B7%9E%E5%B8%82%26c%3D300&from=alamap&tpl=mapcity" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-map-marker" aria-hidden="true"></i>&nbsp;&nbsp;FuZhou,CN</a></li><li><a href="https://love.mrbird.cc" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-diamond" aria-hidden="true"></i>&nbsp;&nbsp;Love</a></li><li><a href="https://cloud.mrbird.cn" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-chain" aria-hidden="true"></i>&nbsp;&nbsp;FEBS</a></li><li><a href="/atom.xml" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-rss" aria-hidden="true"></i>&nbsp;&nbsp;RSS</a></li><li><a href="https://gitee.com/mrbirdd" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-codepen" aria-hidden="true"></i>&nbsp;&nbsp;Gitee</a></li><li><a href="https://github.com/wuyouzhuguli" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-github-alt" aria-hidden="true"></i>&nbsp;&nbsp;GitHub</a></li><li><a href="javascript:;" class="popup-trigger"><i class="fa fa-fw fa-search" aria-hidden="true"></i>&nbsp;&nbsp;Search</a></li></ul></div></div></div><script>var $urls=$(".author__urls").find("a");$urls.each(function(){var o=$(this);o.mouseenter(function(){o.css({color:"#414547"})}),o.mouseleave(function(){o.css({color:""})})})</script></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#静态Pod"><span class="nav-number">1.</span> <span class="nav-text">静态Pod</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置文件方式"><span class="nav-number">1.1.</span> <span class="nav-text">配置文件方式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#HTTP方式"><span class="nav-number">1.2.</span> <span class="nav-text">HTTP方式</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod容器共享Volume"><span class="nav-number">2.</span> <span class="nav-text">Pod容器共享Volume</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ConfigMap"><span class="nav-number">3.</span> <span class="nav-text">ConfigMap</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建ConfigMap"><span class="nav-number">3.1.</span> <span class="nav-text">创建ConfigMap</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Pod容器使用ConfigMap"><span class="nav-number">3.2.</span> <span class="nav-text">Pod容器使用ConfigMap</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Downward-API"><span class="nav-number">4.</span> <span class="nav-text">Downward API</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Pod生命周期"><span class="nav-number">5.</span> <span class="nav-text">Pod生命周期</span></a></li></ol></div></div></section></div></aside></div></main><footer id="footer" class="footer" onselectstart="return!1"><div class="footer-inner"><div class="copyright">&copy; 2016 - <span itemprop="copyrightYear">2019</span>&nbsp;&nbsp; <span class="author" itemprop="copyrightHolder">MrBird</span>&nbsp;&nbsp;|<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>&nbsp;&nbsp;UV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_uv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span> &nbsp;&nbsp;PV&nbsp;<span class="busuanzi-value" id="busuanzi_value_site_pv" style="cursor:pointer" title="统计开始时间：2019年7月5日"></span></div></div></footer><div class="back-to-top"><span style="font-family:'Source Sans Pro','Helvetica Neue',Arial,sans-serif;font-size:1.2em;font-weight:600">TOP</span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":search_path.endsWith("json")&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var C=[];0!=g.length;){var $=g[g.length-1],m=$.position,x=$.word,w=m-20,y=m+80;w<0&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),C.push(r(p,w,y,g))}C.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&(C=C.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",C.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script></body><script>$(function(){$("a").not(".nav-link,.cloud-tie-join-count,.ignore-href,.jstree-anchor").addClass("animsition-link")});var burst1=new mojs.Burst({left:0,top:0,radius:{5:40},children:{shape:"circle",fill:["red","cyan","orange"],fillOpacity:.8,radiusX:3.5,radiusY:3.5}});document.addEventListener("click",function(a){null==a.target.href&&"footer"!=a.target.className&&"copyright"!=a.target.className&&"author__urls social-icons"!=a.target.className&&"author__avatar"!=a.target.className&&"sidebar sidebar-active"!=a.target.className&&burst1.tune({x:a.pageX,y:a.pageY}).generate().replay()})</script><script type="text/javascript" src="/js/message.js"></script></html><!-- rebuild by neat -->